<template>
  <div id="app">
    <headnavbar>
      <!-- #left 语法糖 等同于 name="left" -->
      <template #center>
        <div
          class="return"
          @touchstart="
            (e) => {
              goBack(e);
            }
          "
        >
          <img src="" alt="" />
          <span>返回</span>
        </div>
        <div class="text">
          <p class="big">选择联系人</p>
          <p class="small">你关注的用户</p>
        </div>
      </template>
    </headnavbar>
    <div class="main">
      <div class="search">
        <!-- @keyup.enter 监听回车事件  -->
        <input
          v-model="people"
          @blur="
            () => {
              clearInput();
            }
          "
          @keyup="
            (e) => {
              keyUpSelect(e);
            }
          "
          type="text"
          name=""
          id=""
          placeholder="搜索用户"
        />
      </div>
      <template v-if="isShow && isHavePeople">
        <div :key="index" v-for="(data, index) in nowShowDatas">
          <div class="content peopleMsg">
            <!-- 图片暂时只能些固定的 js动态给的不行 没有服务器 找不到图片 写死的是basse64 格式能正常显示 -->
            <img class="touxiang" src="./image/1.png" alt="" />
            <div class="userText">
              <div style="width: 100%">
                <span style="font-size: 15px">{{ data.name }}</span>
                <!-- 根据性别sex 判断颜色和符号 -->
                <span
                  class="sexAge"
                  :style="
                    data.sex ? 'background: #ef97e0' : 'background: #97BCEF'
                  "
                  >{{ data.sex ? "♀" : "♂" }} {{ data.age }}</span
                >
              </div>
              <span style="font-size: 12px; color: #666666">{{
                data.path
              }}</span>
            </div>
          </div>
        </div>
      </template>
      <template v-else>
        <div class="isHavePeople">查无此人</div>
      </template>
    </div>
  </div>
</template>
<script>
import headnavbar from "../../components/HeadNavBar";
export default {
  sockets: {
    receiveMsg(data) {
      console.log(data);
      let datas = this.$store.state.userinfo.userList;
      // console.log(datas);
      var flag = true;
      if (datas.length === 0) {
        this.$store.commit("setuserList", data);
      } else {
        for (let i = 0; i < datas.length; i++) {
          // console.log(datas[i]);
          if (datas[i].userid === data.userid) {
            datas[i].content = data.content;
            // console.log("第二次发");
            flag = false;
            break;
          }
        }
        if (flag === true) {
          this.$store.commit("setuserList", data);
          // console.log("第一次发");
        }
      }
    },
  },
  data() {
    return {
      people: "",
      isShow: false, // 负责控制主体内容的现实 为了让数据先加载在显示内容
      isHavePeople: true,
      datas: [
        // 假数据
        {
          name: "张丽丽",
          path: "河北",
          age: 18,
          sex: 1, // 0 男    1 女
        },
        {
          name: "大师兄",
          path: "东北",
          age: 88,
          sex: 0,
        },
        {
          name: "二师兄",
          path: "广州",
          age: 44,
          sex: 0,
        },
      ],
      nowShowDatas: [], // 当前需要显示的联系人
    };
  },
  created() {
    this.nowShowDatas = this.datas;
    // console.log(this.people)
    this.isShow = true;
    // console.log(this.isShow)
  },
  components: {
    headnavbar,
  },
  methods: {
    clearInput() {
      // 如果输入的内容为空 或者纯空格 在失去焦点时 清空
      if (this.people.trim() == "") {
        this.people = "";
        this.nowShowDatas = this.datas;
      }
    },
    goBack() {
      // 返回信息主页
      this.$router.push("/Information");
    },
    keyUpSelect() {
      this.nowShowDatas = []; // 每次都清空需要显示的联系人
      // console.log(this.people) // 当前input输入的内容 按下回车后
      for (var i = 0; i < this.datas.length; i++) {
        if (this.datas[i].name.match(this.people.trim())) {
          // console.log(i)
          this.nowShowDatas.push(this.datas[i]);
        }
      }
      if (this.nowShowDatas.length === 0) {
        // 为空说明没有该联系人 显示空
        this.isHavePeople = false;
      } else {
        this.isHavePeople = true;
      }
      // console.log(this.nowShowDatas.length === 0)
    },
  },
};
</script>
<style scoped>
.text {
  margin: 0 auto;
}
.text p {
  margin: 0;
  padding: 0;
}
.text .big {
  font-size: 16px;
}
.text .small {
  font-size: 10px;
  color: #cccccc;
}
.main {
  background: #f0eff5;
  width: 100%;
  height: 100%;
  min-height: 736px;
  z-index: 1;
}
.search {
  width: 90%;
  height: 30px;
  margin: 5px 20px;
}
.search input {
  display: block;
  width: 100%;
  height: 30px;
  border: #e4e4e4;
  border-radius: 5px;
  outline-style: none;
  padding: 0 5px 0 5px;
}
/* 修改input默认显示字的css */
::-webkit-input-placeholder {
  text-align: center;
  color: #cccccc;
}
.main .content {
  background: #ffffff;
  height: 50px;
  border-top: 2px solid #e4e4e4;
  position: relative;
  width: 100;
}
.touxiang {
  width: 15%;
  height: 40px;
  width: 40px;
  position: relative;
  top: 50%;
  transform: translateY(-50%);
  margin-left: 10px;
}
.peopleMsg .userText {
  display: inline-block;
  width: 85%;
}
.sexAge {
  display: inline-block;
  font-size: 12px;
  padding: 1px 2px;
  color: #ffffff;
}
.isHavePeople {
  margin: 20px 0;
  background: #ffffff;
  padding: 20px 30px;
}
</style>
